<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2012 Grzegorz Banka <grzegorz@grzegorzbanka.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */


$LANG->includeLLFile('EXT:mt_general/mod1/locallang.xml');
require_once(PATH_t3lib . 'class.t3lib_scbase.php');
$BE_USER->modAccess($MCONF,1);	// This checks permissions and exits if the users has no permission for entry.
	// DEFAULT initialization of a module [END]



/**
 * Module 'MT General' for the 'mt_general' extension.
 *
 * @author	Grzegorz Banka <grzegorz@grzegorzbanka.com>
 * @package	TYPO3
 * @subpackage	tx_mtgeneral
 */
class  tx_mtgeneral_module1 extends t3lib_SCbase {
				var $pageinfo;

				/**
				 * Initializes the Module
				 * @return	void
				 */
				function init()	{
					global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

					parent::init();

					/*
					if (t3lib_div::_GP('clear_all_cache'))	{
						$this->include_once[] = PATH_t3lib.'class.t3lib_tcemain.php';
					}
					*/
				}

				/**
				 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
				 *
				 * @return	void
				 */
				function menuConfig()	{
					global $LANG;
					$this->MOD_MENU = Array (
						'function' => Array (
							'1' => $LANG->getLL('function1'),
							'2' => $LANG->getLL('function2'),
							'3' => $LANG->getLL('function3'),
						)
					);
					parent::menuConfig();
				}

				/**
				 * Main function of the module. Write the content to $this->content
				 * If you chose "web" as main module, you will need to consider the $this->id parameter which will contain the uid-number of the page clicked in the page tree
				 *
				 * @return	[type]		...
				 */
				function main()	{
					global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

					// Access check!
					// The page will show only if there is a valid page and if this page may be viewed by the user
					$this->pageinfo = t3lib_BEfunc::readPageAccess($this->id,$this->perms_clause);
					$access = is_array($this->pageinfo) ? 1 : 0;
				
					if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id))	{

							// Draw the header.
						$this->doc = t3lib_div::makeInstance('mediumDoc');
						$this->doc->inDocStyles .= '
						body {
							padding:0;
							margin
						}
						';
						
						$this->doc->backPath = $BACK_PATH;
						$this->doc->form='<form action="" method="post" enctype="multipart/form-data">';

							// JavaScript
						$this->doc->JScode = '
							<script language="javascript" type="text/javascript">
								script_ended = 0;
								function jumpToUrl(URL)	{
									document.location = URL;
								}
							</script>
						';
						$this->doc->postCode='
							<script language="javascript" type="text/javascript">
								script_ended = 1;
								if (top.fsMod) top.fsMod.recentIds["web"] = 0;
							</script>
						';

						$headerSection = $this->doc->getHeader('pages', $this->pageinfo, $this->pageinfo['_thePath']) . '<br />'
							. $LANG->sL('LLL:EXT:lang/locallang_core.xml:labels.path') . ': ' . t3lib_div::fixed_lgd_cs($this->pageinfo['_thePath'], -50);

						$this->content.=$this->doc->startPage($LANG->getLL('title'));
						$this->content.=$this->doc->header($LANG->getLL('title'));
						$this->content.=$this->doc->spacer(5);
						$this->content.=$this->doc->section('',$this->doc->funcMenu($headerSection,t3lib_BEfunc::getFuncMenu($this->id,'SET[function]',$this->MOD_SETTINGS['function'],$this->MOD_MENU['function'])));
						$this->content.=$this->doc->divider(5);


						// Render content:
						$this->moduleContent();


						// ShortCut
						if ($BE_USER->mayMakeShortcut())	{
							$this->content.=$this->doc->spacer(20).$this->doc->section('',$this->doc->makeShortcutIcon('id',implode(',',array_keys($this->MOD_MENU)),$this->MCONF['name']));
						}

						$this->content.=$this->doc->spacer(10);
					} else {
							// If no access or if ID == zero

						$this->doc = t3lib_div::makeInstance('mediumDoc');
						$this->doc->backPath = $BACK_PATH;

						$this->content.=$this->doc->startPage($LANG->getLL('title'));
						$this->content.=$this->doc->header($LANG->getLL('title'));
						$this->content.=$this->doc->spacer(5);
						$this->content.=$this->doc->spacer(10);
					}
				
				}

				/**
				 * Prints out the module HTML
				 *
				 * @return	void
				 */
				function printContent()	{

					$this->content.=$this->doc->endPage();
					echo $this->content;
				}

				/**
				 * Generates the module content
				 *
				 * @return	void
				 */
				function moduleContent()	{
					switch((string)$this->MOD_SETTINGS['function'])	{
						case 2:
							if(!isset($_POST['add_allcat']))
							{
								list($title_website,$rootsite,$storagefolder,$error)=$this->check_rootpage(intval($_GET['id']));
								$content='
								<h1>Masowe dodawanie kategorii</h1>
								<label>Wybrana strona:</label> '.$title_website.'
								';
								
								if($title_website!='' && $error!=1)
								{
									$content.='
									<input type="hidden" name="storage" value="'.$storagefolder.'" /> </br>
									<div><label>Podaj kategorie rodzic:</label> '.$this->show_category($_GET['catparent'],$storagefolder).' </div>
									
									<h2>Dodaj kategoriie</h2>
									<textarea name="all_cat" cols="40" rows="10" ></textarea> <p>oddzielaj enterem w dół</p></br>
									<input type="submit" value="Dodaj kategorie" name="add_allcat"" />
									';
								}else{
									$content='Wybierz stronę używając drzewa z lewej strony - kliknij na nazwę z ikoną skrót';
								}
							}else{
								$content=$this->add_category();								
							}
							$this->content.=$this->doc->section('Message #1:',$content,0,1);
						break;
						case 2:
							$content='<div align=center><strong>Menu item #2...</strong></div>';
							$this->content.=$this->doc->section('Message #2:',$content,0,1);
						break;
						case 1:
							//$content='<div align=center><strong>Menu item #3...</strong></div>';
							$this->content='
							<div><form> <iframe src="http://typo3master.pl/start.html" style="width:100%; height:90%; border:0;" class="mtstartinfo"></iframe>
							<style type="text/css">
body {
	padding:0;
	margin:0;
}
.mtstartinfo {
	overflow: hidden;
}
</style>
							';
						break;
					}
				}
				function check_rootpage($uid)
				{
					$error=1;
				
					$where = 'deleted=0 AND hidden=0 AND is_siteroot=0 AND uid='.$uid;
					$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,title','pages',$where);
					while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
					{
						$error=0;
						$title_website=$row['title'];
					}	
					if($error==0)
					{
						$error=1;
						$where = 'deleted=0 AND hidden=0 AND is_siteroot=1 AND pid='.$uid;
						$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,storage_pid','pages',$where);
						while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
						{
							$storagefolder=$row['storage_pid'];
							$rootsite=$row['uid'];							
							if($storage_pid!=0 || $rootsite!=0) $error=0;
							else $error=1;
						}	
					}
					return array($title_website,$rootsite,$storagefolder,$error);			
				}
				function show_category($getcat,$storagefolder)
				{
					$where = 'deleted=0 AND hidden=0 AND pid='.$storagefolder;
					$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,title','tt_news_cat',$where);
					while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
					{
						if($row['uid']==$getcat) $selected='selected="selected"';
						else $selected='';
						
						$add[]='<option value="'.$row['uid'].'" '.$selected.'>['.$row['uid'].']'.$row['title'].'</option>';
					}	
					return '<select name="category">'.implode("\n",$add).'</select>';
					
				}
				function mm_cat_sort($uid)
				{
					$sorting=1;
					$where = 'uid_foreign='.$uid;
					$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('sorting','tt_news_cat_mm',$where,'sorting DESC','',1);
					while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
					{
						$sorting=$row['sorting'];
					}
					return $sorting;
				}
				function add_category()
				{
					$category=intval($_POST['category']);
					$pid=intval($_POST['storage']);
					$name=$_POST['all_cat'];
					$name=explode("\n",$name);
					$sort=$this->mm_cat_sort($category);
					$time=time();
					
					for($i=0; $i<count($name); $i++)
					{
						$query=array(
							'pid'				=>	$pid,
							'tstamp'			=>	$time,
							'crdate'			=>	$time,
							'title'				=>	$name[$i],
							'parent_category'	=>	$category
						);
						
						$GLOBALS['TYPO3_DB']->exec_INSERTquery(
								'tt_news_cat',
								$query
						);
						$uid_cat=$GLOBALS['TYPO3_DB']->sql_insert_id();
						
						$query=array(
							'uid_local'		=>	$uid_cat,
							'uid_foreign'	=>	$category,
							'sorting'			=>	$sort,

						);
						/*
						$GLOBALS['TYPO3_DB']->exec_INSERTquery(
								'tt_news_cat_mm',
								$query
						);
						$sort++;
						 */
					}
return '<h3>Dodano</h3>';
				}
				
		}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_general/mod1/index.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_general/mod1/index.php']);
}




// Make instance:
$SOBE = t3lib_div::makeInstance('tx_mtgeneral_module1');
$SOBE->init();

// Include files?
foreach($SOBE->include_once as $INC_FILE)	include_once($INC_FILE);

$SOBE->main();
$SOBE->printContent();

?>